<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>range_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_math_range_test.js.html">range_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.math.RangeTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.math.RangeTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.math.Range&#39;);
<a name="line19"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line20"></a>
<a name="line21"></a>
<a name="line22"></a>/**
<a name="line23"></a> * Produce legible assertion results. If two ranges are not equal, the error
<a name="line24"></a> * message will be of the form
<a name="line25"></a> * &quot;Expected &lt;[1, 2]&gt; (Object) but was &lt;[3, 4]&gt; (Object)&quot;
<a name="line26"></a> */
<a name="line27"></a>function assertRangesEqual(expected, actual) {
<a name="line28"></a>  if (!goog.math.Range.equals(expected, actual)) {
<a name="line29"></a>    assertEquals(expected, actual);
<a name="line30"></a>  }
<a name="line31"></a>}
<a name="line32"></a>
<a name="line33"></a>function createRange(a) {
<a name="line34"></a>  return a ? new goog.math.Range(a[0], a[1]) : null;
<a name="line35"></a>}
<a name="line36"></a>
<a name="line37"></a>function testFromPair() {
<a name="line38"></a>  var range = goog.math.Range.fromPair([1, 2]);
<a name="line39"></a>  assertEquals(1, range.start);
<a name="line40"></a>  assertEquals(2, range.end);
<a name="line41"></a>  range = goog.math.Range.fromPair([2, 1]);
<a name="line42"></a>  assertEquals(1, range.start);
<a name="line43"></a>  assertEquals(2, range.end);
<a name="line44"></a>}
<a name="line45"></a>
<a name="line46"></a>function testRangeIntersection() {
<a name="line47"></a>  var tests = [[[1, 2], [3, 4], null],
<a name="line48"></a>               [[1, 3], [2, 4], [2, 3]],
<a name="line49"></a>               [[1, 4], [2, 3], [2, 3]],
<a name="line50"></a>               [[-1, 2], [-1, 2], [-1, 2]],
<a name="line51"></a>               [[1, 2], [2, 3], [2, 2]],
<a name="line52"></a>               [[1, 1], [1, 1], [1, 1]]];
<a name="line53"></a>  for (var i = 0; i &lt; tests.length; ++i) {
<a name="line54"></a>    var t = tests[i];
<a name="line55"></a>    var r0 = createRange(t[0]);
<a name="line56"></a>    var r1 = createRange(t[1]);
<a name="line57"></a>    var expected = createRange(t[2]);
<a name="line58"></a>    assertRangesEqual(expected, goog.math.Range.intersection(r0, r1));
<a name="line59"></a>    assertRangesEqual(expected, goog.math.Range.intersection(r1, r0));
<a name="line60"></a>
<a name="line61"></a>    assertEquals(expected != null, goog.math.Range.hasIntersection(r0, r1));
<a name="line62"></a>    assertEquals(expected != null, goog.math.Range.hasIntersection(r1, r0));
<a name="line63"></a>  }
<a name="line64"></a>}
<a name="line65"></a>
<a name="line66"></a>function testBoundingRange() {
<a name="line67"></a>  var tests = [[[1, 2], [3, 4], [1, 4]],
<a name="line68"></a>               [[1, 3], [2, 4], [1, 4]],
<a name="line69"></a>               [[1, 4], [2, 3], [1, 4]],
<a name="line70"></a>               [[-1, 2], [-1, 2], [-1, 2]],
<a name="line71"></a>               [[1, 2], [2, 3], [1, 3]],
<a name="line72"></a>               [[1, 1], [1, 1], [1, 1]]];
<a name="line73"></a>  for (var i = 0; i &lt; tests.length; ++i) {
<a name="line74"></a>    var t = tests[i];
<a name="line75"></a>    var r0 = createRange(t[0]);
<a name="line76"></a>    var r1 = createRange(t[1]);
<a name="line77"></a>    var expected = createRange(t[2]);
<a name="line78"></a>    assertRangesEqual(expected, goog.math.Range.boundingRange(r0, r1));
<a name="line79"></a>    assertRangesEqual(expected, goog.math.Range.boundingRange(r1, r0));
<a name="line80"></a>  }
<a name="line81"></a>}
<a name="line82"></a>
<a name="line83"></a>function testRangeContains() {
<a name="line84"></a>  var tests = [[[0, 4], [2, 1], true],
<a name="line85"></a>               [[-4, -1], [-2, -3], true],
<a name="line86"></a>               [[1, 3], [2, 4], false],
<a name="line87"></a>               [[-1, 0], [0, 1], false],
<a name="line88"></a>               [[0, 2], [3, 5], false]];
<a name="line89"></a>  for (var i = 0; i &lt; tests.length; ++i) {
<a name="line90"></a>    var t = tests[i];
<a name="line91"></a>    var r0 = createRange(t[0]);
<a name="line92"></a>    var r1 = createRange(t[1]);
<a name="line93"></a>    var expected = t[2];
<a name="line94"></a>    assertEquals(expected, goog.math.Range.contains(r0, r1));
<a name="line95"></a>  }
<a name="line96"></a>}
<a name="line97"></a>
<a name="line98"></a>function testRangeClone() {
<a name="line99"></a>  var r = new goog.math.Range(5.6, -3.4);
<a name="line100"></a>  assertRangesEqual(r, r.clone());
<a name="line101"></a>}
<a name="line102"></a>
<a name="line103"></a>function testGetLength() {
<a name="line104"></a>  assertEquals(2, new goog.math.Range(1, 3).getLength());
<a name="line105"></a>  assertEquals(2, new goog.math.Range(3, 1).getLength());
<a name="line106"></a>}
<a name="line107"></a>
<a name="line108"></a>function testRangeContainsPoint() {
<a name="line109"></a>  var r = new goog.math.Range(0, 1);
<a name="line110"></a>  assert(goog.math.Range.containsPoint(r, 0));
<a name="line111"></a>  assert(goog.math.Range.containsPoint(r, 1));
<a name="line112"></a>  assertFalse(goog.math.Range.containsPoint(r, -1));
<a name="line113"></a>  assertFalse(goog.math.Range.containsPoint(r, 2));
<a name="line114"></a>}
<a name="line115"></a>
<a name="line116"></a>function testIncludePoint() {
<a name="line117"></a>  var r = new goog.math.Range(0, 2);
<a name="line118"></a>  r.includePoint(0);
<a name="line119"></a>  assertObjectEquals(new goog.math.Range(0, 2), r);
<a name="line120"></a>  r.includePoint(1);
<a name="line121"></a>  assertObjectEquals(new goog.math.Range(0, 2), r);
<a name="line122"></a>  r.includePoint(2);
<a name="line123"></a>  assertObjectEquals(new goog.math.Range(0, 2), r);
<a name="line124"></a>  r.includePoint(-1);
<a name="line125"></a>  assertObjectEquals(new goog.math.Range(-1, 2), r);
<a name="line126"></a>  r.includePoint(3);
<a name="line127"></a>  assertObjectEquals(new goog.math.Range(-1, 3), r);
<a name="line128"></a>}
<a name="line129"></a>
<a name="line130"></a>function testIncludeRange() {
<a name="line131"></a>  var r = new goog.math.Range(0, 4);
<a name="line132"></a>  r.includeRange(r);
<a name="line133"></a>  assertObjectEquals(new goog.math.Range(0, 4), r);
<a name="line134"></a>  r.includeRange(new goog.math.Range(1, 3));
<a name="line135"></a>  assertObjectEquals(new goog.math.Range(0, 4), r);
<a name="line136"></a>  r.includeRange(new goog.math.Range(-1, 2));
<a name="line137"></a>  assertObjectEquals(new goog.math.Range(-1, 4), r);
<a name="line138"></a>  r.includeRange(new goog.math.Range(2, 5));
<a name="line139"></a>  assertObjectEquals(new goog.math.Range(-1, 5), r);
<a name="line140"></a>  r.includeRange(new goog.math.Range(-2, 6));
<a name="line141"></a>  assertObjectEquals(new goog.math.Range(-2, 6), r);
<a name="line142"></a>}
</pre>


</body>
</html>
